import ScreenDataModelApi from "@/api/screen/core/screen-data-model.js";
import ComponentDef from "@/views/modules/design/right-component/common/component-def.js";
const ChartDef = {
	mixins:[ComponentDef],
	data() {
		return {
			//数据源配置
			dataSource:{},
			//表配置
			tables:[],
			//维度拖拽的父级子级的id
			parentId:'',
			childrenId:'',
			//度量拖拽的父级子级的id
			MrParentId:'',
			MrChildrenId:'',
			//鼠标移上去显示删除按钮
			display: null,
		}
	},
	created() {
		this.init();
	},
	methods: {
		i18n(name,text,key){
			return this.$ti18(name,text,"chartLang",key);
		},
		init() {
			if (this.config.dataSources && this.config.dataSources.dataModel) {
				ScreenDataModelApi.get(this.config.dataSources.dataModel).then(res => {
					if (res.success && res.data && res.data.modelConfig) {
						this.modelConfig = JSON.parse(res.data.modelConfig);
						this.getParamsOptions();
						this.getTableName();
						//获取表配置
						this.tables = JSON.parse(res.data.tables);
					}
					if (res.data.dataSource) {
						this.dataSource = JSON.parse(res.data.dataSource).value;
					}
					this.dataModel = JSON.stringify({
						text: this.config.dataSources.dataModelName || res.data.name,
						value: this.config.dataSources.dataModel
					});
				});
			}
		},
		dimensionStart(index){
			this.parentId = index;
		},
		dimensionEnd(e){
			this.childrenId = e.oldIndex;
			//拖入维度配置
			let index = 0;
			let list = []
			this.modelConfig.dimensions[this.parentId].fields.filter(res=>{
				if(!res.hide){
					list.push(res)
				}
			})
			for (let obj of this.config.dataSources.dimensions) {
				if (list[this.childrenId].fieldName == obj.fieldName &&
					list[this.childrenId].tableName == obj.tableName) {
					index++;
				}
			}
			if(index>=2){
				this.config.dataSources.dimensions.splice(e.newIndex,1);
				this.$notification.open({
					message:this.commonI18n("msa.tips",'提示') ,
					description: this.i18n("msa.repeatDrag","已经拖入了该字段，不能重复拖入"),
					onClick: () => {

					},
				});
			} else if (this.config.chartType == ('map' && "numerical") && this.config.dataSources.dimensions.length >= 2) {
				//漏斗图、地图、雷达图、指数卡只能拖入一个
				this.config.dataSources.dimensions.splice(e.newIndex, 1);
				this.$notification.open({
					message:this.commonI18n("msa.tips",'提示') ,
					description: this.i18n("msa.noMoreThan","不能超过")+'1'+ this.i18n("msa.them","个"),
					onClick: () => {

					},
				});
			}  else if (this.config.dataSources.dimensions.length > 3) {
				this.config.dataSources.dimensions.splice(e.newIndex, 1);
				this.$notification.open({
					message:this.commonI18n("msa.tips",'提示') ,
					description: this.i18n("msa.noMoreThan","不能超过")+'3'+ this.i18n("msa.them","个"),
					onClick: () => {

					},
				});
			}

			//拖入颜色图例
			let colorIndex = 0;
			if(this.config.dataSources.colorLegends){
				for (let obj of this.config.dataSources.colorLegends) {
					if (this.modelConfig.dimensions[this.parentId].fields[this.childrenId].fieldName == obj.fieldName &&
						this.modelConfig.dimensions[this.parentId].fields[this.childrenId].tableName == obj.tableName) {
						colorIndex++;
					}
				}
				if (colorIndex >= 2) {
					this.config.dataSources.colorLegends.splice(e.newIndex, 1);
					this.$notification.open({
						message:this.commonI18n("msa.tips",'提示') ,
						description: this.i18n("msa.repeatDrag","已经拖入了该字段，不能重复拖入"),
						onClick: () => {

						},
					});
				}  else if (this.config.dataSources.colorLegends.length > 3) {
					this.config.dataSources.colorLegends.splice(e.newIndex, 1);
					this.$notification.open({
						message:this.commonI18n("msa.tips",'提示') ,
						description: this.i18n("msa.noMoreThan","不能超过")+'3'+ this.i18n("msa.them","个"),
						onClick: () => {
						},
					});
				}
			}
			//拖入过滤条件配置
			if(this.config.dataSources.filterParams){
				let filterIndex = 0;
				for (let obj of this.config.dataSources.filterParams) {
					if (this.modelConfig.dimensions[this.parentId].fields[this.childrenId].fieldName == obj.fieldName &&
						this.modelConfig.dimensions[this.parentId].fields[this.childrenId].tableName == obj.tableName) {
						filterIndex++;
					}
					//设置默认的条件配置
					if (!obj.condition) {
                        obj["condition"]={controlType: "textbox", opType: "EQ"};
					}
				}
				if (this.config.dataSources.filterParams.length > 1) {
					this.config.dataSources.filterParams.splice(e.newIndex, 1);
					this.$notification.open({
						message:this.commonI18n("msa.tips",'提示') ,
						description: this.i18n("msa.noMoreThan","不能超过")+'1'+ this.i18n("msa.them","个"),
						onClick: () => {
						},
						getContainer:() => document.body
					});
				}
			}
		},
		measureStart(index){
			this.MRParentId = index;
		},
		measureEnd(e){
			this.MrChildrenId = e.oldIndex;
			let index = 0;
			let list = []
			this.modelConfig.measures[this.MRParentId].fields.filter(res=>{
				if(!res.hide){
					list.push(res)
				}
			})
			for (let obj of this.config.dataSources.measures) {
				if (list[this.MrChildrenId].fieldName == obj.fieldName && list[this.MrChildrenId].tableName == obj.tableName) {
					index++;
				}
			}
			if (index >= 2) {
				this.config.dataSources.measures.splice(e.newIndex, 1);
				this.$notification.open({
					message:this.commonI18n("msa.tips",'提示') ,
					description: this.i18n("msa.repeatDrag","已经拖入了该字段，不能重复拖入"),
					onClick: () => {

					},
				});
			} else if (this.config.chartType == ('funnelChart' && 'pieChart')  && this.config.dataSources.measures.length >= 2) {
				//漏斗图只能拖入一个
				this.config.dataSources.measures.splice(e.newIndex, 1);
				this.$notification.open({
					message:this.commonI18n("msa.tips",'提示') ,
					description: this.i18n("msa.noMoreThan","不能超过")+'1'+ this.i18n("msa.them","个"),
					onClick: () => {

					},
				});
			}
			if(!this.config.dataSources.measures2){
				return;
			}
			let index2=0;
			for (let obj of this.config.dataSources.measures2) {
				if (this.modelConfig.measures[this.MRParentId].fields[this.MrChildrenId].fieldName == obj.fieldName && this.modelConfig.measures2[this.MRParentId].fields[this.MrChildrenId].tableName == obj.tableName) {
					index2++;
				}
			}
			if (index2 >= 2) {
				this.config.dataSources.measures2.splice(e.newIndex, 1);
				this.$notification.open({
					message:this.commonI18n("msa.tips",'提示') ,
					description: this.i18n("msa.repeatDrag","已经拖入了该字段，不能重复拖入"),
					onClick: () => {

					},
				});
			}
		}
	},
	watch: {}
}
export default ChartDef;
